package com.bos.restController;

import com.alibaba.fastjson.JSON;
import com.bos.common.PageData;
import com.bos.common.ResultData;
import com.bos.common.StrUtil;
import com.bos.controller.UserController;
import com.bos.data.model.*;
import com.bos.data.repositories.jpa.CustomerJPARepository;
import com.bos.service.ProjectPropertyService;
import com.bos.service.ProjectService;
import com.bos.service.Project_itemSerice;
import com.bos.service.SalesService;
import com.google.common.base.Strings;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * create by luojie 2018/6/11
 */
@RestController
public class RestProjectController {
    //在需要写入日志的地方引入：
    private Logger logger = Logger.getLogger(UserController.class);
    @Autowired
    @Qualifier("projectServiceImpl")
    ProjectService projectService;

    @Autowired
    @Qualifier("salesServiceImpl")
    SalesService salesService;

    @Autowired
    private ProjectPropertyService projectPropertyService;

    @Autowired
    private Project_itemSerice proItemSerice;

    @Autowired
    private CustomerJPARepository customerJPARepository;

    //查出一些通用数据
    @RequestMapping(value = "/findAllData")
    public PageData findAllData(){
        PageData page=new PageData();
        List mapList=new ArrayList<>();
        Map<String,List> map=new HashMap<>();


        List<BosUserModel> users = projectService.findUsers();
        List<ProjectPropertyModel> columns = projectPropertyService.getAllProperty();
        List<ProjectItemModel> proitems = proItemSerice.findAll();
        //查出字典表中的所有的项目进度,工作类别,项目组,订阅人
        List<BosParamModel> progress = projectService.findBos_params("项目进度");
        List<BosParamModel> pType = projectService.findBos_params("工作类别");
        List<BosParamModel> pGroups = projectService.findBos_params("项目组");
        List<BosCustomerModel> customerList=customerJPARepository.findAll();

        map.put("users",users);
        map.put("columns",columns);
        map.put("proitems",proitems);
        map.put("progress",progress);
        map.put("pType",pType);
        map.put("pGroup",pGroups);
        map.put("customers",customerList);
        mapList.add(map);
        page.setData(mapList);
        return page;
    }


    // 1.查询出Project表中的数据
    @RequestMapping(value = "/showAll1")
    public ResultData findAll(HttpSession session) {
        ResultData result=new ResultData();
        int pageNo = 1;
        PageData data = projectService.findWithTerm(null, null, null, null, null, PageData.DEFAULTPAGESIZE, pageNo);

        List<BosUserModel> users = projectService.findUsers();
        List<ProjectPropertyModel> columns = projectPropertyService.getAllProperty();
        List<ProjectItemModel> proitems = proItemSerice.findAll();
        //查出字典表中的所有的项目进度,工作类别,项目组,订阅人
        List<BosParamModel> progress = projectService.findBos_params("项目进度");
        List<BosParamModel> pType = projectService.findBos_params("工作类别");
        List<BosParamModel> pGroups = projectService.findBos_params("项目组");
        List<BosCustomerModel> customerList=customerJPARepository.findAll();
        session.setAttribute("users", users);
        session.setAttribute("list", data);
        session.setAttribute("column", columns);
        session.setAttribute("proItems", proitems);
        session.setAttribute("progress", progress);
        session.setAttribute("pType", pType);
        session.setAttribute("pGroups", pGroups);
        session.setAttribute("customerList",customerList);
        result.setResult("true");
        result.setMessage("查询成功");
        result.setPageData(data);
        return result;
    }

    @RequestMapping(value = "/delProject/{id}")
    public ResultData delProject(@PathVariable("id") int id){
        ResultData result=new ResultData();
        projectService.deleteById(id);
        result.setMessage("删除成功");
        result.setResult("true");
        return result;
    }

    @RequestMapping(value = "/addProject")
    public ResultData addProject(){
        ResultData result=new ResultData();


        return result;
    }

    @RequestMapping(value = "selectByAll1" ,method = RequestMethod.POST)
    public ResultData sendData(HttpServletRequest request, HttpSession session, Model model){
        ResultData result=new ResultData();
        try {
            int pageNo = 1, pageSize = PageData.DEFAULTPAGESIZE;
            String ds = request.getParameter("ds");
            Map map = JSON.parseObject(ds);

            //获取页面的当前页数和页面大小,默认是第一页
            String pageIndex =map.get("pageNo").toString();// request.getParameter("pageNo");
            String setPageSize = map.get("setPageSize").toString();//request.getParameter("setPageSize");
            if (!Strings.isNullOrEmpty(pageIndex)) {
                pageNo = Integer.valueOf(pageIndex);
            }
            if (!Strings.isNullOrEmpty(setPageSize)) {
                pageSize = Integer.valueOf(setPageSize);
            }
            String dstatus=map.get("dstatus").toString();//request.getParameter("dstatus");
            String leaders="";
            String status="";
            List<String> sleaderList=new ArrayList();
            List<String> statusList=new ArrayList();
            for (Object mapData : map.entrySet()) {
                Map.Entry<String,List> entry = (Map.Entry<String,List>)mapData;
                if(entry.getKey().equals("leader")){
                    leaders=entry.getValue().toString();
                }
                if(entry.getKey().equals("status")){
                    status=entry.getValue().toString();
                }
            }
            if(leaders!=null){
                sleaderList=StrUtil.splitStrToList(leaders);
            }
            if(status!=null){
                statusList=StrUtil.splitStrToList(status);
            }

            PageData sales = salesService.findSales(sleaderList, statusList, pageSize, pageNo);
            for (Object o : sales.getData()) {

            }
            model.addAttribute("list", sales);
            if(Strings.isNullOrEmpty(dstatus)){
                session.setAttribute("dstatus","none");
            }else {
                session.setAttribute("dstatus",dstatus);
            }

            Map newMap=new HashMap();
            newMap.put("sleader",leaders);
            newMap.put("stype",status);

            result.setPageData(sales);
            List<Map> mapList=new ArrayList<>();
            mapList.add(newMap);
            result.setOtherData(mapList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
}
